﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TERP.WaterClient.Common;
using System.Data;
using CommonUtils;
//using TERP.WaterClient.Entities.Auth;
//using TERP.WaterClient.Entities.Customer;

namespace TERP.WaterClient.DbServices
{
    public class AuthService:BaseService
    {
        private const string MSG_INVALID_SIGNIN_RETURN = "Invalid sign-in return";
		private const string MSG_INVALID_USER = "Invalid user";
        private const string MIX_ITEM_TID_ORG = "GetNewId";
		private const string MIX_ITEM_DB_DT = "GetDateTimeDB";
		//
		private const string TBL_USER = "Sys_User";

        public AuthService():base(){

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strUserCode"></param>
        /// <param name="strPassword"></param>
        /// <returns>
        /// xxx|yyy = session id|usercode
        /// </returns>
        public string[] Signin(string strUserCode,string strPassword){
            string strTId = "";
			string strNickname = strUserCode; //????
            //
            try
            {
                DataSet mdsFinal = _dbService.CommonSignIn(
                    _strGwUserCode, _strGwPassword,
                    strTId, _strPartnerCode,
                    strUserCode, _strLangCode,"",
                    strNickname,
                    strPassword,"");
                //
                mdsFinal = TERP.Utils.CUtils.MyDSDecode(mdsFinal);
                if (CMyDataSet.HasError(mdsFinal))
                {
                    throw GenServiceException(mdsFinal);
                }
                //
                string strReturn = Convert.ToString(CMyDataSet.GetRemark(mdsFinal));
                string[] result = strReturn.Split('|');
                if (result == null || result.Length != 2)
                {
                    throw new Exception(MSG_INVALID_SIGNIN_RETURN);
                }
                return result;
            }
            catch (Exception ex){
                throw ex;
            }
        }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="strUserCode"></param>
		/// <returns></returns>
        //public User GetUserInfo(string strUserCode)
        //{
        //    string strTId = SystemGlobal.Instance.GetNextTid();
        //    try
        //    {
        //        DataSet mdsFinal = _dbService.SysGetUser(
        //            _strGwUserCode, _strGwPassword,
        //           strTId, _strSessionId,
        //           strUserCode,
        //           "",
        //           "",
        //           "",
        //           "");
        //        if (CMyDataSet.HasError(mdsFinal))
        //        {
        //            throw GenServiceException(mdsFinal);
        //        }
        //        //
        //        DataTable dtblUser = mdsFinal.Tables[TBL_USER];
        //        if (dtblUser.Rows.Count != 1)
        //            throw new Exception(MSG_INVALID_USER);
        //        Dealer dealer = new Dealer();
        //        User user = new User();
        //        user.Dealer = dealer;
        //        //
        //        DataRow dtRow = dtblUser.Rows[0];
        //        user.DealerCode = Convert.ToString(dtRow[TblUser.Dealer_Code]);
        //        user.UserCode = Convert.ToString(dtRow[TblUser.User_Code]);
        //        user.UserName = Convert.ToString(dtRow[TblUser.User_Name]);
        //        user.FlagActive = Convert.ToString(dtRow[TblUser.Flag_Active]);
        //        user.Area.AreaCode = Convert.ToString(dtRow[TblDealer.Area_Code]);
        //        user.Area.AreaName = ""; //not return yet
        //        //
        //        dealer.AreaCode = Convert.ToString(dtRow[TblDealer.Area_Code]);
        //        dealer.DealerCode = Convert.ToString(dtRow[TblDealer.Dealer_Code]);
        //        dealer.BUCode = Convert.ToString(dtRow[TblDealer.BU_Code]);
        //        dealer.BUPattern = Convert.ToString(dtRow[TblDealer.Bu_Pattern]);
        //        dealer.DealerName = Convert.ToString(dtRow[TblDealer.Dealer_Name]);
        //        dealer.DealerStatus = Convert.ToString(dtRow[TblDealer.Dealer_Status]);
        //        dealer.DealerScale = Convert.ToString(dtRow[TblDealer.Dealer_Scale]);
        //        dealer.PhoneNo = Convert.ToString(dtRow[TblDealer.Phone_No]);
        //        dealer.FaxNo = Convert.ToString(dtRow[TblDealer.Fax_No]);
        //        dealer.CompanyName = Convert.ToString(dtRow[TblDealer.Cpn_Name]);
        //        dealer.CompanyAddress = Convert.ToString(dtRow[TblDealer.Cpn_Address]);
        //        dealer.ShowroomAddress = Convert.ToString(dtRow[TblDealer.Sr_Address]);
        //        dealer.GarageAddress = Convert.ToString(dtRow[TblDealer.Gr_Address]);
        //        dealer.GaragePhoneNo = Convert.ToString(dtRow[TblDealer.Gr_Phone_No]);
        //        dealer.GarageFaxNo = Convert.ToString(dtRow[TblDealer.Gr_Fax_No]);
        //        dealer.DirectorName = Convert.ToString(dtRow[TblDealer.Director_Name]);
        //        dealer.DirectorEmail = Convert.ToString(dtRow[TblDealer.Director_Email]);
        //        dealer.DirectorPhoneNo = Convert.ToString(dtRow[TblDealer.Director_Phone_No]);
        //        dealer.SalesManagerName = Convert.ToString(dtRow[TblDealer.Sale_Mng_Name]);
        //        dealer.SalesManagerPhoneNo = Convert.ToString(dtRow[TblDealer.Sale_Mng_Phone_No]);
        //        dealer.SalesManagerEmail = Convert.ToString(dtRow[TblDealer.Sale_Mng_Email]);
        //        dealer.GarageManagerName = Convert.ToString(dtRow[TblDealer.Gr_Mng_Name]);
        //        dealer.GarageManagerPhoneNo = Convert.ToString(dtRow[TblDealer.Gr_Mng_Phone_No]);
        //        dealer.GarageManagerEmail = Convert.ToString(dtRow[TblDealer.Gr_Mng_Email]);
        //        dealer.TaxCode = Convert.ToString(dtRow[TblDealer.Tax_Code]);
        //        dealer.ContactName = Convert.ToString(dtRow[TblDealer.Contact_Name]);
        //        dealer.Remark = Convert.ToString(dtRow[TblDealer.Remark]);
        //        //
        //        return user;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strUserCode"></param>
        /// <returns></returns>
        public string GetTIdOrg(){
			string strTId = "";
			try
			{
				DataSet mdsFinal = _dbService.CommonGetMixItem(
					_strGwUserCode, _strGwPassword,
				   strTId, _strPartnerCode,
				   _strUserCode, _strLangCode,
				   MIX_ITEM_TID_ORG, null);
                mdsFinal = TERP.Utils.CUtils.MyDSDecode(mdsFinal);
				if (CMyDataSet.HasError(mdsFinal))
				{
					throw GenServiceException(mdsFinal);
				}
				//
				string strReturn = Convert.ToString(CMyDataSet.GetRemark(mdsFinal));
				return strReturn;
			}
			catch (Exception ex)
			{
				throw ex;
			}
        }

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public DateTime GetServerDateTime(){
			string strTId = SystemGlobal.Instance.GetNextTid();
			try
			{
				DataSet mdsFinal = _dbService.CommonGetMixItem(
					_strGwUserCode, _strGwPassword,
				   strTId, _strPartnerCode,
				   _strUserCode, _strLangCode,
				   MIX_ITEM_DB_DT, null);

                mdsFinal = TERP.Utils.CUtils.MyDSDecode(mdsFinal);
				if (CMyDataSet.HasError(mdsFinal))
				{
					throw GenServiceException(mdsFinal);
				}
				//
				string strReturn = Convert.ToString(CMyDataSet.GetRemark(mdsFinal));
				DateTime dt = DateTime.Parse(strReturn);
				return dt;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

    }
}
